<?php
/**
 * @title 1774. 最接近目标价格的甜点成本
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $baseCosts
     * @param Integer[] $toppingCosts
     * @param Integer $target
     * @return Integer
     */
    function closestCost($baseCosts, $toppingCosts, $target) {
        /**
         * @since 2024-05-28 返回最接近 target 的甜点成本。如果有多种方案，返回 成本相对较低 的一种
         * @since 2024-05-28 甜点数组长度<10，可以暴力破解，3^10=59049
         * @since 2024-05-28 甜品的可能性
         */
        $near = null;
        $diff = PHP_INT_MAX;
        $dp = self::AllNums($toppingCosts);

        /**
         * @since 2024-05-30 双循环，找出接近目标值
         */
        foreach ($baseCosts as $b){
            foreach ($dp as $n){
                $sum = $b + $n;
                $d = abs($sum - $target);
                if($d < $diff) {
                    $diff = $d;
                    $near = $sum;
                } elseif($d == $diff) {
                    $near = min($near, $sum);
                } else {}
            }
        }

        /**
         * @return
         */
        return $near;
    }

    /**
     * @title 返回所有甜品的可能性
     * @author start2004
     * @since 2024-05-30 Created
     *
     * @param Array $toppingCosts
     * @param Array $dp
     * @return Array
     */
    function AllNums($toppingCosts, $dp = [0]){
        if(empty($toppingCosts)) return $dp;

        /**
         * @since 2024-05-30 选一个单品出来
         * @since 2024-05-30 三种可能性，0份，1份，2份
         */
        $num = array_pop($toppingCosts);
        $nextDp = [];
        foreach ($dp as $n){
            $nextDp[] = $n;
            $nextDp[] = $n+$num;
            $nextDp[] = $n+2*$num;
        }

        /**
         * @return
         */
        return self::AllNums($toppingCosts, $nextDp);
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.28_12.php
 */
$datas = [
    // [[1,7], [3,4], 10], // 10
    // [[3,10], [2,5], 9], // 8
    [[9,10,1], [1,8,8,1,1,8], 8], // 7
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->closestCost(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
